package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.PerformancePlanRequestVO;
import com.ugoodtech.mdcc.client.dto.PerformancePlanVO;
import com.ugoodtech.mdcc.client.dto.ServerResponse;
import com.ugoodtech.mdcc.client.exception.BizException;
import com.ugoodtech.mdcc.client.service.PerformancePlanService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import javax.validation.Valid;


@RestController
@RequestMapping("/v1")
public class PerformancePlanController {

	@Autowired
	private PerformancePlanService performancePlanService;

	/**
	 * 添加业绩报告信息
	 */
	@RequestMapping(value = "/plan", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse addPlan(
			 @RequestBody PerformancePlanRequestVO performancePlanRequestVO) {
		try {
			performancePlanService
					.addOrUpdatePerformancePlan(performancePlanRequestVO.getRegionManager(),
							performancePlanRequestVO.getPlotManager(), performancePlanRequestVO.getYear(),
							performancePlanRequestVO.getPerformancePlanSpecificData());
		}catch (BizException e){
			return ServerResponse.error(ServerResponse.MSG_FAILED,e.getErrMsg());
		}
		return ServerResponse.success(ServerResponse.MSG_SUCCESS, new HashMap<>(0));
	}

	@RequestMapping(value = "/plan", method = RequestMethod.GET)
	public ServerResponse getDataOfEachType(@RequestParam Integer year) {
		PerformancePlanVO dataNeedInput;
		try {
			dataNeedInput = performancePlanService.getDataNeedInput(year);
		} catch (BizException e) {
			e.printStackTrace();
			return ServerResponse.error(ServerResponse.MSG_FAILED,e.getErrMsg());
		}
		return ServerResponse
				.success(ServerResponse.MSG_SUCCESS, dataNeedInput);
	}

	/**
	 * 获取用户报告年份分类信息
	 */
	@RequestMapping(value = "/plan/year", method = RequestMethod.GET)
	public ServerResponse getYearGroup() {
		return ServerResponse
				.success(ServerResponse.MSG_SUCCESS, performancePlanService.getYearGroupInfo());
	}
}
